const express = require('express')
let app=express()
app.listen(3000,()=>{console.log('3000ok')})


const formidable=require("formidable")
const path = require('path');
const fs = require('fs');
const timeStamp = require('time-stamp');

// const uuid=require("uuid")
// console.log(uuid.v4());
let {v4}=require("uuid")


app.use("/uploads",express.static( path.join(__dirname,"./uploads")  ))



app.post("/uploads",(req,res)=>{

   
    let form=formidable({
        uploadDir: path.join(__dirname,"./tempdir") 
    })

    form.parse(req,(err,fields,files)=>{

       
        if(err){
            res.send("文件解析失败")
            return
        }

       

        let oldpath=files.touxiang.filepath

        // 后缀名
        let extname=path.extname(files.touxiang.originalFilename)

        let newname= "xiaoming"  +extname


        let newpath=path.join(__dirname,"./uploads",newname)
        



        try {
            fs.renameSync(oldpath,newpath)
        } catch (error) {
            if(error){
                res.send("文件上传失败")
                return
            }
        }

        res.send("文件上传成功")

    })    
    console.log(33333333);
})



/* {
    "touxiang": {
        "size": 2233,
        "filepath": "C:\\Users\\IBM\\Desktop\\软件学院0906\\day13express中间件\\代码\\文件上传\\tempdir\\fdae7b1ea5214c066bf909400",
        "newFilename": "fdae7b1ea5214c066bf909400",
        "mimetype": "image/png",
        "mtime": "2022-04-01T07:57:47.646Z",
        "originalFilename": "cts-微信.png"
    }
} */